﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ThuChi
{
    public partial class QuanLyMuc : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int maQuyen;
            try
            {
                maQuyen = Utilities.GetMaQuyen((int)Session["MaTaiKhoan"]);

            }
            catch //(Exception ex)
            {
                maQuyen = 0;
            }

            if (maQuyen == 0)
            {
                Session["RefUrl"] = "~/QuanLyMuc.aspx";
                Response.Redirect("~/Login.aspx");
            }
        }

        protected void grvMuc_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                e.Row.Cells[0].Text = "Trang " + (grvMuc.PageIndex + 1) + " của " + grvMuc.PageCount;
            }
        }

        protected void grvMuc_RowDeleted(object sender, GridViewDeletedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblErrorMessage.Text = "Không thể xóa mục " + e.Values[0].ToString();// + " \n" + e.Exception.Message.ToString();
                e.ExceptionHandled = true;
            }
            else
            {
                lblErrorMessage.Text = "";
            }
        }

        protected void cboPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            grvMuc.PageSize = grvMuc.PageSize = Convert.ToInt16(cboPageSize.SelectedValue);
        }


        protected void DetailsViewMuc_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
        {
            if (e.Exception != null)
            {
                lblErrorMessage.Text = "Vui lòng nhập tên mục!";
                e.ExceptionHandled = true;
            }
            else
            {
                grvMuc.DataBind();
                grvMuc.PageIndex = grvMuc.PageCount - 1;
                lblErrorMessage.Text = "";
                e.ExceptionHandled = false;
            }
            DetailsViewMuc.Focus();
        }

        protected void LinqDataSourceMuc_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            cboPageSize_SelectedIndexChanged(sender, e);
        }

        protected void grvMuc_PreRender(object sender, EventArgs e)
        {
            grvMuc.DataBind();
        }
    }
}